﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Text;
using System.Web.Mvc.Ajax;

namespace springProject.Extentions
{
    public static class Paging
    {
        public static MvcHtmlString PagingNavigator(this HtmlHelper helper, int pageNumber, int pagesCount, int pageSize)
        {
            StringBuilder sb = new StringBuilder();

            if (pageNumber > 0)
            {
                sb.Append(helper.ActionLink("<", "Index", new { pageNumber = pageNumber - 1 }));
            }
            else
            {
                sb.Append(HttpUtility.HtmlEncode("<"));
            }
            sb.Append(HttpUtility.HtmlEncode(""));

            int pagesNumber = (int)Math.Ceiling((double)pagesCount / pageSize);

            if (pageNumber < pagesCount - 1)
            {
                sb.Append(helper.ActionLink(">", "Index", new { pageNumber = pageNumber + 1 }));
            }
            else
            {
                sb.Append(HttpUtility.HtmlEncode(">"));
            }

            return MvcHtmlString.Create(sb.ToString());
        }

        public static MvcHtmlString PagingNavigator(this HtmlHelper helper, int pageNumber, int pagesCount, int pageSize, string action, string controller, Guid Id)
        {
            StringBuilder sb = new StringBuilder();

            if (pageNumber > 0)
            {
                sb.Append(helper.ActionLink("<туда", action, controller, new { id = Id, pageNum = pageNumber - 1 }, new { }));
            }
            else
            {
                sb.Append(HttpUtility.HtmlEncode("<туда"));
            }
            sb.Append(HttpUtility.HtmlEncode(" | " + (pageNumber + 1).ToString() + " | "));

            int pagesNumber = (int)Math.Ceiling((double)pagesCount / pageSize);

            if (pageNumber < pagesNumber - 1)
            {
                sb.Append(helper.ActionLink("сюда>", action, controller, new { id = Id, pageNum = pageNumber + 1 }, new { }));
            }
            else
            {
                sb.Append(HttpUtility.HtmlEncode("сюда>"));
            }

            return MvcHtmlString.Create(sb.ToString());
        }

        public static MvcHtmlString PagingNavigator(this AjaxHelper helper, int pageNumber, int pagesCount, int pageSize, string action, string controller)
        {
            StringBuilder sb = new StringBuilder();
            AjaxOptions ao = new AjaxOptions();
            ao.UpdateTargetId = "PartialPart";

            if (pageNumber > 0)
            {
                sb.Append(helper.ActionLink("<туда", action, controller, new { pageNumber = pageNumber - 1 }, ao));
            }
            else
            {
                sb.Append(HttpUtility.HtmlEncode("<туда"));
            }
            sb.Append(HttpUtility.HtmlEncode(" | " + (pageNumber + 1).ToString() + " | "));

            int pagesNumber = (int)Math.Ceiling((double)pagesCount / pageSize);

            if (pageNumber < pagesNumber - 1)
            {
                sb.Append(helper.ActionLink("сюда>", action, controller, new { pageNumber = pageNumber + 1 }, ao));
            }
            else
            {
                sb.Append(HttpUtility.HtmlEncode("сюда>"));
            }

            return MvcHtmlString.Create(sb.ToString());
        }

        public static MvcHtmlString PagingNavigator(this AjaxHelper helper, int pageNumber, int pagesCount, int pageSize, string action, string controller,Guid filmId)
        {
            StringBuilder sb = new StringBuilder();
            AjaxOptions ao = new AjaxOptions();
            ao.UpdateTargetId = "PartialPart";

            if (pageNumber > 0)
            {
                sb.Append(helper.ActionLink("<туда", action, controller, new {filmId = filmId, pageNumber = pageNumber - 1 }, ao));
            }
            else
            {
                sb.Append(HttpUtility.HtmlEncode("<туда"));
            }
            sb.Append(HttpUtility.HtmlEncode(" | " + (pageNumber + 1).ToString() + " | "));

            int pagesNumber = (int)Math.Ceiling((double)pagesCount / pageSize);

            if (pageNumber < pagesNumber - 1)
            {
                sb.Append(helper.ActionLink("сюда>", action, controller, new {filmId = filmId, pageNumber = pageNumber + 1 }, ao));
            }
            else
            {
                sb.Append(HttpUtility.HtmlEncode("сюда>"));
            }

            return MvcHtmlString.Create(sb.ToString());
        }
    }
}